{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/TexasInstruments/edgeai-tidl-tools/blob/experiment/examples/jupyter_notebooks/colab/infer_ort.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "tsPipOjCU0_S",
        "vscode": {
          "languageId": "python"
        }
      },
      "outputs": [],
      "source": [
        "!pip install onnxruntime\n",
        "!wget https://akm-img-a-in.tosshub.com/indiatoday/images/story/201804/jet.jpeg\n",
        "!wget https://github.com/onnx/models/blob/1008b6e9450ab632538390e38e0ab28b3f207fbc/vision/classification/squeezenet/model/squeezenet1.1-7.onnx?raw=true\n",
        "!mv squeezenet1.1-7.onnx?raw=true squeezenet1.1-7.onnx"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dzyP9uIaPCol",
        "vscode": {
          "languageId": "python"
        }
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from PIL import Image\n",
        "\n",
        "import onnxruntime as rt\n",
        "sess_options = rt.SessionOptions()\n",
        "sess_options.enable_profiling = True\n",
        "\n",
        "model_path = \"./squeezenet1.1-7.onnx\"\n",
        "input_file=\"./jet.jpeg\"\n",
        "EP_list = ['CPUExecutionProvider']\n",
        "session = rt.InferenceSession(model_path, providers=EP_list, sess_options=sess_options)\n",
        "\n",
        "\n",
        "# get the name of the first input of the model\n",
        "input_name = session.get_inputs()[0].name\n",
        "input_details  = session.get_inputs()\n",
        "output_details = session.get_outputs()\n",
        "\n",
        "#Pre-Process input\n",
        "image    = Image.open(input_file).resize((224, 224))\n",
        "img_data = np.array(image).transpose(2, 0, 1).astype('float32')\n",
        "mean_vec   = np.array([0.485, 0.456, 0.406])\n",
        "stddev_vec = np.array([0.229, 0.224, 0.225])\n",
        "norm_img_data = np.zeros(img_data.shape).astype('float32')\n",
        "for i in range(img_data.shape[0]):\n",
        "    norm_img_data[i,:,:] = (img_data[i,:,:]/255 - mean_vec[i]) / stddev_vec[i]\n",
        "input_data = norm_img_data.reshape(1, 3, 224, 224)\n",
        "\n",
        "#Run inference session\n",
        "raw_result = session.run([], {input_name: input_data})\n",
        "\n",
        "#Post Process Result\n",
        "res = np.array(raw_result[0]).reshape(-1)\n",
        "print(res.argsort()[-5:][::-1])\n",
        "\n",
        "prof_file = session.end_profiling()\n",
        "print(prof_file)\n"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "include_colab_link": true,
      "name": "infer_ort.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
